﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO.IsolatedStorage;
using System.IO;
using System.Linq;
using System.Xml.Linq;
using BiHCurrencyConverter.Model;
using BiHCurrencyConverter.Resources;
using System.Globalization;

namespace BiHCurrencyConverter
{
    public class Utility
    {
        const double bamCourse = 1.95583;
        const double eurCourse = 1.0;

        public static void InitializeOrUpdateSettings()
        {
            IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;

            using (IsolatedStorageFile fileStore = IsolatedStorageFile.GetUserStoreForApplication())
            {
                IsolatedStorageFileStream streamRead = fileStore.OpenFile("eurofxref-daily.xml", FileMode.OpenOrCreate);
                //StreamReader readFile = null;
                try
                {
                    XDocument dataDoc = XDocument.Load(streamRead);
                    XNamespace gesmes = "http://www.ecb.int/vocabulary/2002-08-01/eurofxref";
                    var quotes = from rootNode in dataDoc.Descendants(gesmes + "Cube")
                                 from dateNode in rootNode.Descendants(gesmes + "Cube")
                                 from cubeNode in dateNode.Descendants(gesmes + "Cube")
                                 select new Currency { Symbol = cubeNode.Attribute("currency").Value, Rate = double.Parse(cubeNode.Attribute("rate").Value.Replace(".", CultureInfo.CurrentCulture.NumberFormat.CurrencyDecimalSeparator)) }; ;

                    if (!settings.Contains("BAM"))
                    {
                        settings["BAM"] = bamCourse;
                    }

                    if (!settings.Contains("EUR"))
                    {
                        settings["EUR"] = eurCourse;
                    }

                    foreach (var item in quotes)
                    {
                        settings[item.Symbol] = item.Rate;

                    }
                }
                catch (Exception)
                {
                    MessageBox.Show(AppResources.msgUnableToPerf);

                }

            }
        }

        double SetupExchangeRateFormat(double sourceRate)
        {
            NumberFormatInfo nfi = CultureInfo.CurrentCulture.NumberFormat;
            nfi = (NumberFormatInfo)nfi.Clone();
            //nfi.CurrencySymbol = "";
            string numSeparator = nfi.CurrencyDecimalSeparator;
            string tmpString;
            double returnValue;
            if (numSeparator != ".")
            {
                tmpString = sourceRate.ToString().Replace(".", numSeparator);
                returnValue = Convert.ToDouble(tmpString);
            }
            else
            {
                returnValue = sourceRate;
            }

            return returnValue;


        }



    }
}
